/*  KDevelop 4.3.1
 *  FindtheMin.cxx
 *
 * 	Author : Pallab
 * 	Created on: 12:54:54 PM - 2013:1:26
 * 	"I have not failed, I have just found 10000 ways that won't work."
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <numeric>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;


#define foR(i,frm,upto) for( int i=(frm),_i=(upto);(i) < (_i) ; ++i )
#define foD(i,frm,upto) for( int i=(frm),_i=(upto);(i) >= (_i) ; --i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define dbg(...)       printf(__VA_ARGS__)
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define line puts("")

inline void wait ( double seconds ) {
    double endtime = clock() + ( seconds * CLOCKS_PER_SEC );
    while ( clock() < endtime ) {
        ;
    }
}
template<class T>
    inline T fastIn() {
    register char c=0;
    register T a=0;
    bool neg=false;
    while ( c<33 ) c=getchar();
    while ( c>33 ) {
        if ( c=='-' ) {
            neg=true;
        } else {
            a= ( a*10 ) + ( c-'0' );
        }
        c=getchar();
    }
    return neg?-a:a;
}
int const MXN= ( int ) 1e5+5;
deque<Int> numbers;
deque<Int> cycle;
int n,k;
Int a,b,c,r;
inline void read() {
    numbers.clear();
    n=fastIn<int>(),k=fastIn<int>();
    a=fastIn<Int>(),b=fastIn<Int>(),c=fastIn<Int>(),r=fastIn<Int>();
}
unordered_map<Int,int> mp;
multiset<Int> st;
multiset<Int> ::iterator it;
inline void generateNumbers() {
    st.clear();
    mp.clear();
    numbers.push_back ( a );
    mp[a]++;

    foR ( i,1,k ) {
        numbers.push_back ( ( b*numbers[i-1]+c ) %r );
        mp[ numbers.back() ]++;
    }
    foR ( i,0, ( 1000000 ) ) {
        if ( mp[i]>0 ) {
            ;
        } else {
            st.insert ( i );
        }
    }
}
inline Int getNext ( ) {
    return ( *st.begin() ) ;
}
inline Int solve() {
    int cycleLength = SZ ( cycle );
    return cycle[ n%cycleLength ];

}
inline void proc() {
    generateNumbers();
    cycle.clear();
    Int toRemove,toAdd;
    //dbg ( "result = %lld %lld\n",toRemove,toAdd );
    for ( int itr=0,i=k; itr<=k ; ++itr,++i ) {
        toRemove = numbers.front();
        toAdd = getNext();
        cycle.push_back ( toAdd );
        //1:
        st.erase ( st.begin() ) ;
        mp[toAdd]++;
        numbers.push_back ( toAdd );


        //2:
        numbers.pop_front();
        int &tmp = mp[toRemove];
        --tmp;
        if ( tmp<=0 ) {
            st.insert ( toRemove );
        }
        if ( i+1==n ) {
            cout<<toAdd<<"\n";
            return ;
        }
    }
    Int retval = solve();
    cout<<retval<<"\n";
}
int main() {
    int kase = 1;
    char const *__IN="outputs.out";
    char const *__OUT="out3";
#if defined( xerxes_pc )
    freopen ( __IN, "r", stdin );
    freopen ( __OUT , "w" ,stdout );
    kase = 1;
#endif
    kase=fastIn<int>();
    for ( int i=1; i<=kase; ++i ) {
        read();
        cout<<"Case #"<<i<<": ";
        proc();
    }
    return 0;
}

// kate: indent-mode cstyle; indent-width 4; replace-tabs on; 








